using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for UIActionsControl.
	/// </summary>
	public class UIActionsControl : System.Windows.Forms.UserControl
	{
    private FreeFlow.WinFormsControls.Bevel bevel1;
    private System.Windows.Forms.Panel buttonPanel;
    private System.Windows.Forms.ToolTip toolTip;
    private System.Windows.Forms.Panel actionsPanel;
    private System.Windows.Forms.Label actionsLabel;
    private System.Windows.Forms.Panel alertsPanel;
    private System.Windows.Forms.Label alertsLabel;
    private System.Windows.Forms.Label resultLabel;
    private System.Windows.Forms.Panel alertHeaderPanel;
    private System.ComponentModel.IContainer components;

		public UIActionsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.actionsPanel = new System.Windows.Forms.Panel();
      this.actionsLabel = new System.Windows.Forms.Label();
      this.bevel1 = new FreeFlow.WinFormsControls.Bevel();
      this.buttonPanel = new System.Windows.Forms.Panel();
      this.toolTip = new System.Windows.Forms.ToolTip(this.components);
      this.alertsPanel = new System.Windows.Forms.Panel();
      this.resultLabel = new System.Windows.Forms.Label();
      this.alertHeaderPanel = new System.Windows.Forms.Panel();
      this.alertsLabel = new System.Windows.Forms.Label();
      this.actionsPanel.SuspendLayout();
      this.alertsPanel.SuspendLayout();
      this.alertHeaderPanel.SuspendLayout();
      this.SuspendLayout();
      // 
      // actionsPanel
      // 
      this.actionsPanel.BackColor = System.Drawing.SystemColors.ControlDark;
      this.actionsPanel.Controls.Add(this.actionsLabel);
      this.actionsPanel.Dock = System.Windows.Forms.DockStyle.Top;
      this.actionsPanel.Location = new System.Drawing.Point(2, 88);
      this.actionsPanel.Name = "actionsPanel";
      this.actionsPanel.Size = new System.Drawing.Size(148, 32);
      this.actionsPanel.TabIndex = 0;
      // 
      // actionsLabel
      // 
      this.actionsLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.actionsLabel.Location = new System.Drawing.Point(8, 8);
      this.actionsLabel.Name = "actionsLabel";
      this.actionsLabel.Size = new System.Drawing.Size(136, 24);
      this.actionsLabel.TabIndex = 0;
      this.actionsLabel.Text = "Actions";
      // 
      // bevel1
      // 
      this.bevel1.Dock = System.Windows.Forms.DockStyle.Left;
      this.bevel1.Location = new System.Drawing.Point(0, 0);
      this.bevel1.Name = "bevel1";
      this.bevel1.Shape = System.Windows.Forms.Border3DSide.Left;
      this.bevel1.Size = new System.Drawing.Size(2, 328);
      this.bevel1.TabIndex = 1;
      this.bevel1.Text = "bevel1";
      // 
      // buttonPanel
      // 
      this.buttonPanel.BackColor = System.Drawing.SystemColors.ControlLightLight;
      this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Fill;
      this.buttonPanel.Location = new System.Drawing.Point(2, 120);
      this.buttonPanel.Name = "buttonPanel";
      this.buttonPanel.Size = new System.Drawing.Size(148, 208);
      this.buttonPanel.TabIndex = 2;
      // 
      // alertsPanel
      // 
      this.alertsPanel.BackColor = System.Drawing.SystemColors.ControlLightLight;
      this.alertsPanel.Controls.Add(this.resultLabel);
      this.alertsPanel.Controls.Add(this.alertHeaderPanel);
      this.alertsPanel.Dock = System.Windows.Forms.DockStyle.Top;
      this.alertsPanel.Location = new System.Drawing.Point(2, 0);
      this.alertsPanel.Name = "alertsPanel";
      this.alertsPanel.Size = new System.Drawing.Size(148, 88);
      this.alertsPanel.TabIndex = 1;
      this.alertsPanel.Visible = false;
      // 
      // resultLabel
      // 
      this.resultLabel.Dock = System.Windows.Forms.DockStyle.Fill;
      this.resultLabel.Location = new System.Drawing.Point(0, 32);
      this.resultLabel.Name = "resultLabel";
      this.resultLabel.Size = new System.Drawing.Size(148, 56);
      this.resultLabel.TabIndex = 2;
      this.resultLabel.Text = "resultLabel";
      // 
      // alertHeaderPanel
      // 
      this.alertHeaderPanel.BackColor = System.Drawing.SystemColors.ControlDark;
      this.alertHeaderPanel.Controls.Add(this.alertsLabel);
      this.alertHeaderPanel.Dock = System.Windows.Forms.DockStyle.Top;
      this.alertHeaderPanel.Location = new System.Drawing.Point(0, 0);
      this.alertHeaderPanel.Name = "alertHeaderPanel";
      this.alertHeaderPanel.Size = new System.Drawing.Size(148, 32);
      this.alertHeaderPanel.TabIndex = 1;
      // 
      // alertsLabel
      // 
      this.alertsLabel.BackColor = System.Drawing.SystemColors.ControlDark;
      this.alertsLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.alertsLabel.Location = new System.Drawing.Point(8, 8);
      this.alertsLabel.Name = "alertsLabel";
      this.alertsLabel.Size = new System.Drawing.Size(136, 24);
      this.alertsLabel.TabIndex = 0;
      this.alertsLabel.Text = "Alerts";
      // 
      // UIActionsControl
      // 
      this.Controls.Add(this.buttonPanel);
      this.Controls.Add(this.actionsPanel);
      this.Controls.Add(this.alertsPanel);
      this.Controls.Add(this.bevel1);
      this.Name = "UIActionsControl";
      this.Size = new System.Drawing.Size(150, 328);
      this.actionsPanel.ResumeLayout(false);
      this.alertsPanel.ResumeLayout(false);
      this.alertHeaderPanel.ResumeLayout(false);
      this.ResumeLayout(false);

    }
		#endregion

    private UIActionList actions;
    public UIActionList Actions
    {
      get
      {
        return actions;
      }
      set
      {
        actions = value;
        // create buttons
        buttonPanel.SuspendLayout();
        try
        {
          buttonPanel.Controls.Clear();
          if (actions != null)
          {
            for (int i = actions.Count-1; i >= 0; i--)
            {
              UIAction action = actions[i];
              Label label;
              if ((action.EventHandler == null) && (action.ExecuteDelegate == null))
              {
                // group heading
                label = new Label();
                label.BackColor = SystemColors.ControlLight;
              }
              else
              {
                // actions
                label = new LinkLabel();
                ((LinkLabel)label).LinkBehavior = LinkBehavior.HoverUnderline;
                toolTip.SetToolTip(label, action.Hint);
                if (action.ExecuteDelegate != null)
                  label.Tag = action.ExecuteDelegate;
                else
                  label.Tag = action.EventHandler;
                label.Click += new EventHandler(ExecuteAction);
              }
              buttonPanel.Controls.Add(label);
              label.Dock = DockStyle.Top;
              label.Text = action.Caption;
              label.TextAlign = ContentAlignment.MiddleLeft;
              label.Size = new Size(20, 20);
            }
          }
        }
        finally
        {
          buttonPanel.ResumeLayout();
        }
      }
    }

    private void ExecuteAction(object sender, System.EventArgs e)
    {
      LinkLabel label = (LinkLabel)sender;
      if (label.Tag is ActionExecuteDelegate)
      {
        ActionExecuteDelegate executeDelegate = (ActionExecuteDelegate)label.Tag;
        string text = executeDelegate();
        SetFeedback(text);
      }
    }

    public void SetFeedback(string text)
    {
      alertsPanel.Visible = (!string.IsNullOrEmpty(text));
      resultLabel.Text = text;
        
      if (!resultLabel.IsDisposed)
      {
        // resize the panel
        Graphics graphics = resultLabel.CreateGraphics();
        SizeF size = graphics.MeasureString(text, resultLabel.Font, resultLabel.Width);
        alertsPanel.Height = (int)size.Height + alertHeaderPanel.Height;
      }
    }
	}
}
